package mvv.learn.spring.controller;

import java.util.ArrayList;
import java.util.List;

import mvv.learn.spring.model.PersonInforIfc;
import mvv.learn.spring.service.MyTestService;
import mvv.learn.spring.utilities.AppParam;
import mvv.learn.spring.utilities.ResResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/test")
public class WelcomeController {
	private static final Logger log = LoggerFactory.getLogger(WelcomeController.class);

	@Autowired
	private PersonInforIfc personInforIfc;
	@Autowired
	private MyTestService testService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String greeting() {
		return "default";
	}

	@RequestMapping(value = "/json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public List<String> testJson() {
		ArrayList<String> array = new ArrayList<String>();
		array.add("Hello");
		array.add("the");
		array.add("world");
		array.add("!");
		return array;
	}

	@RequestMapping(value = "/helloTheWorld", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResResponse helloTheWorld() {
		ResResponse res = null;
		try {
			res = testService.helloTheWorld();
		}catch (Exception e) {
			log.error(e.getMessage(), e);
			res = new ResResponse(AppParam.V_BAD, e.getMessage(), e);
		}

		return res;
	}
}
